#include <stdio.h>
#include "user_uart.h"
#include "string.h"
#include "app.h"


#define TAG "uart"

#define USER_UART_NUM UART_NUM_2
#define UART_2_TX GPIO_NUM_17
#define UART_2_RX GPIO_NUM_16
#define UART_BUFFER_SIZE 100

//static uint8_t uart_buffer[UART_BUFFER_SIZE];

extern tSys_Flag Sys_Flag;
extern tSys_Status Sys_Status;
SemaphoreHandle_t sem_Uart_Get_Data;

static QueueHandle_t uart_queue;

void uart_init(void)
{
    uart_config_t uart_cfg = {
        .baud_rate = 115200,                   // 波特率115200
        .data_bits = UART_DATA_8_BITS,         // 8位数据位
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, // 无硬件流控制
        .parity = UART_PARITY_DISABLE,         // 无校验位
        .stop_bits = UART_STOP_BITS_1,          // 1位停止位
        .source_clk = UART_SCLK_APB
        
    };
    uart_param_config(USER_UART_NUM, &uart_cfg);

    gpio_config_t gpio_cfg =
        {
            .pin_bit_mask = (1 << UART_2_RX | 1 << UART_2_TX),
            .pull_up_en = GPIO_PULLUP_ENABLE    // IO 上拉使能，否则接收失败
        };
    gpio_config(&gpio_cfg);
    
    uart_set_pin(USER_UART_NUM, UART_2_TX, UART_2_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // 设置引脚,tx为17,rx为16
    uart_driver_install(USER_UART_NUM, 1024, 1024, 20, &uart_queue, 0); // 安装串口驱动
}

void User_Uart_Send_Data(char *buff)
{
    int len = 0;
    if((len = strlen(buff)) < UART_LEN_MAX)
    {
        uart_write_bytes(USER_UART_NUM, buff, len);
    }
}

void uart_event_task(void *pvParameters)
{
    uart_event_t event;

    uart_init();

    sem_Uart_Get_Data = xSemaphoreCreateBinary();   // 创建显示更新信号量

    while (1)
    {
        // Waiting for UART event.
        if (xQueueReceive(uart_queue, (void *)&event, (TickType_t)portMAX_DELAY))
        {
            //ESP_LOGI(TAG, "uart[%d] event:", USER_UART_NUM);
            switch (event.type)
            {
            case UART_DATA:
                //ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
                uart_read_bytes(USER_UART_NUM, Sys_Status.Uart_Data_Buff, event.size, portMAX_DELAY);
                Sys_Status.Uart_Data_Buff[event.size] = 0x00;
                xSemaphoreGive(sem_Uart_Get_Data);

                break;
            // Event of HW FIFO overflow detected
            case UART_FIFO_OVF:
                ESP_LOGI(TAG, "hw fifo overflow");
                uart_flush_input(USER_UART_NUM);
                xQueueReset(uart_queue);
                break;
            // Event of UART ring buffer full
            case UART_BUFFER_FULL:
                ESP_LOGI(TAG, "ring buffer full");
                // If buffer full happened, you should consider increasing your buffer size
                // As an example, we directly flush the rx buffer here in order to read more data.
                uart_flush_input(USER_UART_NUM);
                xQueueReset(uart_queue);
                break;
            // Event of UART RX break detected
            case UART_BREAK:
                ESP_LOGI(TAG, "uart rx break");
                break;
            // Event of UART parity check error
            case UART_PARITY_ERR:
                ESP_LOGI(TAG, "uart parity error");
                break;
            // Event of UART frame error
            case UART_FRAME_ERR:
                ESP_LOGI(TAG, "uart frame error");
                break;
            default:
                ESP_LOGI(TAG, "uart event type: %d", event.type);
                break;
            }
        }
        vTaskDelay(1);
    }
    // vTaskDelay(NULL);
}
